<?php 

function InsertContactUs()
{
    global $dbsql,$language;
    require_once "includes/php-captcha.inc.php";
    
    if (PhpCaptcha::Validate($_POST['SpamCode'])==false)
    {
         AjaxTransfer('spam',getLang('Please enter the correct security code!'));
    }
    
	$ip              = $_SERVER['REMOTE_ADDR'];
	$postdate        = date("Y/m/d");
    
    //Personal Details
	$Name 		= gpc_getStringPost('Name');
	$Email		= gpc_getStringPost('Email');
    $Subject    = gpc_getStringPost('Subject');
    $Message    = gpc_getStringPost('Message');
    
    ImportClass('account');
    if(!isValidEmail($Email))
    {
        AjaxTransfer('err','Please enter validate email');
    }
    $emailconfig       = @getConfigEmail('ContactUsEmail');
    
    if($emailconfig==false)
    {
        include('admin/source/emailtemp/functions.php');
        InsertConfigEmail('ContactUsEmail','ContactUsEmail','ContactUsEmail','pntrinh87@gmail.com','content');
        //AjaxTransfer('config','Email not config');
    }
    
    $sql_insert = "INSERT INTO tblfeedback
	   (cName,cEmail,cTitle,cDescription,iStatus,dPostDate,cLanguage) 
	VALUES 
	('$Name','$Email','$Subject','$Message','0','$postdate','$language')";
	
	$dbsql->query($sql_insert);	
    
    $subject                = $emailconfig->title;
    $subject                = str_ireplace('{NAME}',$Name,$subject);
    $subject                = str_ireplace('{email}',$Email,$subject);
    $subject                = str_ireplace('{SUBJECT}',$Subject,$subject);
    $emailconfig->content   = str_ireplace('{NAME}',$Name,$emailconfig->content);
    $emailconfig->content   = str_ireplace('{EMAIL}',$Email,$emailconfig->content);
    $emailconfig->content   = str_ireplace('{SUBJECT}',$Subject,$emailconfig->content);
    $emailconfig->content   = str_ireplace('{MESSAGE}',$Message,$emailconfig->content);
    $emailconfig->content  .= '<BR>IP: '.$ip;
    
 	//send to admin
 	$rs   = send_email($emailconfig->email,"",$subject,$emailconfig->content);
 	if($rs==false)
    {
        //AjaxTransfer('ok','Send Email False please contact to admin');
    }
    
    $datasuccess    = getConfig('ContactUsSuccess');
    if($datasuccess==false)
    {
        $datasuccess = getLang('Send success. Thanks your feedback!');
    }
    $datasuccess    = str_replace('{NAME}',$Name,$datasuccess);
    
	AjaxTransfer('ok',$datasuccess);
	
}
?>